// 2025/10/28
// 最大正方形

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        vector<vector<int>> square(m, vector<int>(n));
        int ans = 0;
        for(int i = 0; i < m; i++)
        {
            square[i][0] = matrix[i][0] - '0';
            ans = max(ans, square[i][0]);
        }
        for(int j = 1; j < n; j++)
        {
            square[0][j] = matrix[0][j] - '0';
            ans = max(ans, square[0][j]);
        }
        
        for(int i = 1; i < m; i++)
        {
            for(int j = 1; j < n; j++)
            {
                if(matrix[i][j] == '1')
                {
                    square[i][j] = min(square[i][j - 1], square[i - 1][j]);
                    square[i][j] = min(square[i][j], square[i - 1][j - 1]) + 1;
                    ans = max(ans, square[i][j]);
                }
            }
        }
        return ans * ans;
    }
};